package org.jmap.geom;

import org.jmap.core.Util;
import org.osgeo.proj4j.CRSFactory;
import org.osgeo.proj4j.CoordinateReferenceSystem;

/**
 * a <code>Curve</code> is a multi-point whos points are assumed to be 
 * connected
 * 
 * @author Ali
 *
 */
public class Curve extends MultiPoint {

	@Override
	public double getGeodesicLength(CoordinateReferenceSystem projection) {
		Geometry geom = null;
		try {
			geom = (Geometry) this.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		
		if (projection != null) {
			CRSFactory factory = new CRSFactory();			
			CoordinateReferenceSystem wgs84 = factory.createFromName("EPSG:4326");
			
			if (!wgs84.equals(projection)) {
				geom = transform(projection, wgs84);
			}
		}
		
		double length = 0.0d;
		if (hasComponents()) {
			for (int i = 1; i <= components.size(); i++) {
				Point p1 = (Point) components.get(i - 1);
				Point p2 = (Point) components.get(i);
				
				length += Util.geodeticDistance(p1.toMapPoint(), p2.toMapPoint());
			}
		}
		
		return length * 1000;
	}

	@Override
	public double getLength() {
		double length = 0.0d;
		if (hasComponents()) {
			for (int i = 1; i <= components.size(); i++) {
				length += this.components.get(i - 1).distanceTo(this.components.get(i), false);
			}
		}
		
		return length;
	}

	
}
